/**
 * 
 */
package com.wisdytech.linkdcs.equipment.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.linkdcs.system.model.SysResource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.session.UserSession;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipment.model.EquipModel;
import com.wisdytech.linkdcs.equipment.service.IEquipmentModelService;
import com.wisdytech.linkdcs.equipment.vo.EquipmentModelVO;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * <p>
 * ClassName: EquipTypeAction
 * </p>
 * <p>
 * Description: 设备类别管理前台交互Action层
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-26
 * </p>
 */
@Controller
@RequestMapping("/equipment/type")
public class EquipTypeAction {
    
    /**
     * 设备类别service
     */
    @Autowired
    @Qualifier("equipmentModelService")
    private IEquipmentModelService equipmentModelService;

    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    
    
    /**
     * 
     * <p>Description: 设备类别管理</p>
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model , HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "equipment/equipRegistered/modelMange/index";
    }
    
    
    /**
     * 分页条件查询设备类别管理页面信息
     * 
     * @param modelCode 设备类别编号
     * @param modelType 设备类别型号
     * @param filter 分页参数
     * @return 分页查询结果集
     * @throws IOException 
     */
    @RequestMapping(value = "/listEuipmentModel/get.mvc")
    @ResponseBody
    public StandardTableResult<EquipmentModelVO> listEuipmentModel(String emModel, String emModelType, Filter filter) throws IOException {
//    	String newNmModelType = new String(emModelType.getBytes("iso8859-1"),"UTF-8");
    	//通过查询条件查询所有设备类别信息并分页
       PageInfo<EquipModel> equipModels = equipmentModelService.listModelByFilter(emModel, emModelType, filter);
       //转换为前台结果集
       List<EquipModel> modelList = equipModels.getList();
       List<EquipmentModelVO> modelVOList=new ArrayList<EquipmentModelVO>();
       for(EquipModel model: modelList){
           modelVOList.add(DataTransformUtil.changeModelVO(model));
       }
       return new StandardTableResult<EquipmentModelVO>(equipModels.getTotal(),modelVOList);
    }
    /**
     * <p>Description: 点击修改时带出对应设备类别信息</p> 
     * @param pkId 设备类别唯一标识pkId
     * @return 设备类别VO对象
     */
    @RequestMapping(value = "/getEuipmentModelById/get.mvc")
    @ResponseBody
    public EquipmentModelVO getEuipmentModelById(String pkId){
        EquipModel model = equipmentModelService.getEuipmentModelById(pkId);
        EquipmentModelVO modelVO=new EquipmentModelVO();
        modelVO = DataTransformUtil.changeModelVO(model);
        return modelVO;
    }
    /**
     * <p>Description: 通过设备类别id删除对应信息</p> 
     * @param pkId 设备类别id
     * @return 删除结果对象
     */
    @RequestMapping(value = "/delEuipmentModelById/del.mvc")
    @ResponseBody
    public StandardResult delEuipmentModelById(String pkId) {
        return equipmentModelService.delModel(pkId);
    }
    /**
     * <p>Description: 保存设备类别对象</p> 
     * @param model 设备类别对象
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveEquipmentModel/save.mvc")
    @ResponseBody
    public StandardResult saveEquipmentModel(EquipModel model, HttpSession session) {
       SysUser userSession = BaseUtil.getCurrentUser();
       String loginName = userSession.getUsername();
       
        if (StringUtils.isBlank(model.getPkId())) {
            return equipmentModelService.addModel(model, loginName);
        }else {
            return equipmentModelService.updateModel(model, loginName);
        }
    }
    
    /**
     * <p>Description:查询设备类别编号信息列表</p> 
     * @return 设备类别编号信息列表集合
     */
    @RequestMapping(value = "/listAllModel/get.mvc")
    @ResponseBody
    public List<EquipModel> listAllModel(@ModelAttribute("search") String search){
        return equipmentModelService.listAllModel(search);
    }
    
    /**
     * <p>Description:查询设备类别编号信息列表</p> 
     * @return 设备类别编号信息列表集合
     */
    @RequestMapping(value = "/listAllSupplier/get.mvc")
    @ResponseBody
    public List<EquipModel> listAllSupplier(@ModelAttribute("search") String search){
        return equipmentModelService.listAllSupplier(search);
    }
    
    /**
     * <p>Description:查询设备类别类型信息列表</p> 
     * @return 设备类别类型信息列表集合
     */
    @RequestMapping(value = "/listAllModelType/get.mvc")
    @ResponseBody
    public List<EquipModel> listAllModelType(@ModelAttribute("search") String search){
        return equipmentModelService.listAllModelType(search);
    }
    
    /**
     * <p>Description:查询设备类别编号信息列表</p> 
     * @return 设备类别编号信息列表集合
     */
    @RequestMapping(value = "/getEquipmentModelByEmModel/get.mvc")
    @ResponseBody
    public EquipmentModelVO getEquipmentModelByEmModel(String emModel){
        EquipmentModelVO modelVO = new EquipmentModelVO(); 
        EquipModel model = equipmentModelService.getEuipmentModelByEmModel(emModel);
        if (model != null) {
            modelVO = DataTransformUtil.changeModelVO(model);
        }
        return modelVO;
    }
    
    
    
    /**
     * <p>Description:查询设备类别编号信息和参数信息列表</p> 
     * @return 设备类别编号信息和参数信息集合
     */
    @RequestMapping(value = "/listParamByEmModel/get.mvc")
    @ResponseBody
    public List<EquipmentModelVO> listParamByEmModel(String emModel){
        List<EquipModel> modelList = equipmentModelService.listParamByEmModel(emModel);
        List<EquipmentModelVO> modelVOList=new ArrayList<EquipmentModelVO>();
        for(EquipModel model: modelList){
            modelVOList.add(DataTransformUtil.changeModelVO(model));
        }
        return modelVOList;
    }

}
